import axios from 'axios'
import commonUrl from '../common/js/commonUrl.js'
import { loadUserId } from '../common/js/cache'
import qs from 'qs'

// 获取姓名和电话号码
export function getUser () {
  const url = `${commonUrl.apihost}index.php/home/Proprietorauth/getUser`
  const data = {
    user_id: loadUserId()
  }
  return axios({
    method: 'POST',
    url: url,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 获取验证码
export function getIndentCode (phoneNum) {
  const url = `${commonUrl.apihost}index.php/home/Proprietorauth/sendCode`
  const data = {
    phone: phoneNum,
    user_id: loadUserId()
  }
  return axios({
    method: 'POST',
    url: url,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 房屋信息的数据
export function getHomeDate (id) {
  const url = `${commonUrl.apihost}index.php/home/Proprietorauth/getHouses?areas_id=${id}`
  return axios({
    method: 'GET',
    url: url
  }).then(res => {
    return Promise.resolve(res.data)
  }).catch(err => {
    return Promise.reject(err)
  })
}

// 获取房号的信息
export function getRoomNum (id) {
  const url = `${commonUrl.apihost}index.php/home/Proprietorauth/getUnit`
  const data = {
    danyuan_id: id
  }
  return axios({
    method: 'POST',
    url: url,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 提交业主认证信息
export function subData (code, id, area, houseNum, did, serverId, phone, other) {
  const url = `${commonUrl.apihost}index.php/home/Proprietorauth/add`
  const data = {
    code: code,
    user_id: loadUserId(),
    areasId: id,
    area: area,
    house_num: houseNum,
    danyuan_id: did,
    serverIds: serverId,
    phone: phone,
    other: other
  }
  return axios({
    method: 'POST',
    url: url,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 认证列表数据
export function getidentList () {
  const url = `${commonUrl.apihost}index.php/home/Proprietorauth/getDetail`
  const data = {
    user_id: loadUserId()
  }
  return axios({
    method: 'POST',
    url: url,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 认证详情页面
export function getidentDetail (id) {
  const url = `${commonUrl.apihost}index.php/home/Proprietorauth/getOneDetail`
  const data = {
    id: id
  }
  return axios({
    method: 'POST',
    url: url,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 认证详情页面删除
export function deleteOne (id) {
  const url = `${commonUrl.apihost}index.php/home/Proprietorauth/deleteOne`
  const data = {
    id: id
  }
  return axios({
    method: 'POST',
    url: url,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}

// 我的投票详情
export function getResult (id, type) {
  const url = `${commonUrl.apihost}/index.php/home/my_vote/getDetail`
  const data = {
    userId: loadUserId(),
    type: type,
    id: id
  }
  return axios({
    method: 'POST',
    url: url,
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    data: qs.stringify(data)
  }).then((res) => {
    return Promise.resolve(res.data)
  }).catch((err) => {
    return Promise.reject(err)
  })
}
